package com.uwlaser.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

    /**
     * Druid监控
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet());
        //访问路径
        bean.addUrlMappings("/druid/*");

        Map<String,Object> initParams = new HashMap<>();
        //这是配置的druid监控的登录密码
        //initParams.put("loginUsername","root");
        //initParams.put("loginPassword","root");

        //默认就是允许所有访问
        initParams.put("allow","127.0.0.1");
        //黑名单IP
        initParams.put("deny","192.168.15.21");
        // 是否能够重置数据.
        initParams.put("reset-enable","true");

        bean.setInitParameters(initParams);
        return bean;
    }

    /**
     * web监控的filter
     */
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
        bean.addUrlPatterns("/*");
        bean.addInitParameter("exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return  bean;
    }
}
